//
//  HDCFAccountSetVc.m
//  HaoDeCaiFu
//
//  Created by 郑开宏 on 2017/11/3.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFAccountSetVc.h"
#import "HDCFSettingCell.h"
#import "HDCFMangerPwVc.h"
#import "HDCFRiskTestVc.h"
#import "HDCFOpenAccountVc.h"
#import "HDCFMyInfoVc.h"
#import "HDCFMyBankCardVc.h"
#import "SVProgressHUD.h"
#import "HDCFRiskLevelVc.h"
#import "HDCFcommenQuestionVc.h"

#define KPersonalInfo @"/public/investor/get-investor-info"

@interface HDCFAccountSetVc ()<HDCFOpenAccountVcDelegate>

@property (nonatomic, strong) NSArray *imageAry;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSDictionary *personalInfo;

@end

@implementation HDCFAccountSetVc

- (NSArray *)imageAry{
    if (!_imageAry) {
        self.imageAry = [NSArray arrayWithObjects:@"icon_geren", @"icon_kaihu", @"icon_ka", @"icon_fengxian", @"icon_mima", @"icon_wenti", @"icon_dianhua", @"711512180619_.pic_hd.jpg", @"icon_tuichu", nil];
    }
    return _imageAry;
}

- (NSArray *)titleArray{
    if (!_titleArray) {
        self.titleArray = [NSArray arrayWithObjects:@"个人信息", @"开户", @"银行卡", @"风险能力测试", @"密码管理", @"常见问题", @"客服电话", @"当前版本号", @"退出账户", nil];
    }
    return _titleArray;
}

- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

- (void)getMineInfo{
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    paramets[@"accountNumber"] = kAcount;
    NSString *jsonStr = [self dictionaryToJson:paramets];
    NSDictionary *paramet = @{KparametOne:jsonStr,
                              KparametTwo:KPersonalInfo};
    
    [HttpRequest sendGetRequestWithURL:nil WithParamets:paramet success:^(id result) {
        NSString *code = result[@"code"];
        if ([code isEqualToString:@"0000"]) {
            NSDictionary *dataDict = result[@"data"];
            self.personalInfo = dataDict;
            NSNumber *riskRating = dataDict[@"riskAppetite"];
            if (riskRating.integerValue > 0) {
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                [user setValue:riskRating forKey:@"riskRating"];
                [user synchronize];
            }
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
        
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"账户设置"];
    self.view.backgroundColor = ZKcolor(245, 245, 245);
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.separatorColor = ZKcolor(245, 245, 245);
    if ([kOpenStatus isEqualToString:@"1"]) {
        [self getMineInfo];
    }
    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshAccountSet) name:@"refreshAccountSet" object:nil];
}

- (void)refreshAccountSet{
    [self getMineInfo];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellid";
    HDCFSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [HDCFSettingCell HDCFSettingCell];
    }
    NSString *imgName= self.imageAry[indexPath.row];
    cell.imgView.image = [UIImage imageNamed:imgName];
    
    NSString *title = self.titleArray[indexPath.row];
    cell.titleLable.text = title;
    
    if (indexPath.row == 1) {
        
        if ([kOpenStatus isEqualToString:@"1"]) {
            cell.detailLabel.text = @"已开户";
        }else{
            cell.detailLabel.text = @"未开户";
        }
    }else if (indexPath.row == 3) {
        if (self.personalInfo) {
            if ([self.personalInfo[@"riskAppetite"] isEqual:@0]) {
                cell.detailLabel.text = @"未评测";
            }else{
                NSNumber *riskAppetite = self.personalInfo[@"riskAppetite"];
                if (riskAppetite.integerValue == 1) {
                    cell.detailLabel.text = @"保守型";
                }else if (riskAppetite.integerValue == 2){
                    cell.detailLabel.text = @"中度保守型";
                }else if (riskAppetite.integerValue == 3){
                    cell.detailLabel.text = @"平衡型";
                }else if (riskAppetite.integerValue == 4){
                    cell.detailLabel.text = @"中度进取型";
                }else{
                    cell.detailLabel.text = @"进取型";
                }
            }
        }else{
            cell.detailLabel.text = @"未评测";
        }
    }else if(indexPath.row == 6){
        cell.detailLabel.text = @"0379-60667637";
        
    }else if(indexPath.row == 7){
        NSString *key = @"CFBundleVersion";
        // 获得当前软件的版本号
        NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
        cell.detailLabel.text = [NSString stringWithFormat:@"%.1f", currentVersion.doubleValue];
        
    }else{
        cell.detailLabel.hidden = YES;
    }
    
    if (indexPath.row == 8) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }else{
       cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.imageAry.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0){
        HDCFMyInfoVc *vc = [[HDCFMyInfoVc alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if (indexPath.row == 3) {
        if (self.personalInfo) {
            if ([self.personalInfo[@"riskAppetite"] isEqual:@0]) {
                HDCFRiskTestVc *vc = [[HDCFRiskTestVc alloc] init];
                vc.isNormal = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                NSNumber *riskLevel = self.personalInfo[@"riskAppetite"];
                HDCFRiskLevelVc *vc = [[HDCFRiskLevelVc alloc] init];
                vc.isRoot = YES;
                vc.riskLevel = riskLevel.integerValue;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }else{
            NSString *openStatus = kOpenStatus;
            if (!(openStatus.integerValue > 0)){
                [SVProgressHUD showInfoWithStatus:@"请先开户"];
                [SVProgressHUD dismissWithDelay:1.0];
                return;
            }
        }
    }else if (indexPath.row == 4){
        HDCFMangerPwVc *vc = [[HDCFMangerPwVc alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 1){
        NSString *openStatus = kOpenStatus;
        if (openStatus.integerValue > 0){
            [SVProgressHUD showInfoWithStatus:@"您已开户，不能重复开户"];
            [SVProgressHUD dismissWithDelay:1.0];
            return;
        }
        HDCFOpenAccountVc *vc = [[HDCFOpenAccountVc alloc] init];
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 8){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您确定要退出吗？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString *key = @"CFBundleVersion";
            NSString *lastVersion = [user stringForKey:key];
            
            //删除所有userdefault的信息
            NSDictionary *dict = [user dictionaryRepresentation];
            for(id key in dict) {
                [user removeObjectForKey:key];
            }
            [user setValue:lastVersion forKey:key];
            [user synchronize];
            if ([self.delegate respondsToSelector:@selector(quitSucess)]) {
                [self.delegate quitSucess];
            }
            [self.navigationController popViewControllerAnimated:YES];
        }];
        UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction: action1];
        [alert addAction: action2];
        [self presentViewController:alert animated:NO completion:nil];
    }else if (indexPath.row == 2){
        NSString *openStatus = kOpenStatus;
        if (!(openStatus.integerValue > 0)){
            [SVProgressHUD showInfoWithStatus:@"请先开户"];
            [SVProgressHUD dismissWithDelay:1.0];
            return;
        }
        HDCFMyBankCardVc *vc = [[HDCFMyBankCardVc alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 6){
        NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
        if (phoneVersion.integerValue >= 10) {
            NSString *phoneNumberStr = @"0379-60667637";
            NSString *str = [NSString stringWithFormat:@"tel://%@", phoneNumberStr];
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:str]];
        }else{
            NSString *phoneNumberStr = @"0379-60667637";
            NSString *contentStr = [NSString stringWithFormat:@"拨打电：%@", phoneNumberStr];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"拨打电话" message:contentStr delegate:self cancelButtonTitle:@"不要" otherButtonTitles:@"拨打", nil];
            [alertView show];
        }
    }else if (indexPath.row == 5){
        HDCFcommenQuestionVc *vc = [[HDCFcommenQuestionVc alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)openAccountSucess{
    [self getMineInfo];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
